package tech.summerly.weather.component.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import kotlinx.android.synthetic.main.item_daily_forecast_vertical.view.*
import tech.summerly.weather.R
import tech.summerly.weather.data.bean.DailyForecast
import tech.summerly.weather.data.remote.CaiYunService
import tech.summerly.weather.util.ViewHolder
import tech.summerly.weather.util.formatDate
import tech.summerly.weather.util.getWeekDay
import tech.summerly.weather.util.isToday
import java.util.*

class DailyForecastAdapter(private val dailyForecast: DailyForecast) : RecyclerView.Adapter<ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(R.layout.item_daily_forecast_vertical, parent, LayoutInflater.from(parent.context))
    }

    override fun getItemCount(): Int {
        return dailyForecast.temperature.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) = with(holder.itemView) {
        val temperature = dailyForecast.temperature[position]
        val calendar = temperature.date.formatDate()
        if (calendar.isToday()) {
            textDay.text = "%s(%s)".format(context.getString(R.string.today), calendar.getWeekDay())
        } else {
            textDay.text = context.getString(R.string.month_day_template).format(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH))
        }
        val skyCon = dailyForecast.skycon[position].value
        textDesc.text = context.getString(R.string.daily_desc).format(CaiYunService.SkyCon.getSkyConLocalString(skyCon), temperature.max, temperature.min)
        icon.setImageResource(CaiYunService.SkyCon.getSkyIcon(skyCon))
        Unit
    }

}